package cn.diancan.util;

import java.io.IOException;
import java.io.PrintWriter; 
import java.util.Iterator; 
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse; 
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 这个类的方法。不要直接调用 (除开BaseAction 和少数的情况下)
 * @author Sky
 *
 */
public class ResponseJSONUtil {
	
	public static final Logger log = LoggerFactory.getLogger(ResponseJSONUtil.class);
	
	/**
	 * 输出json
	 * @param response 
	 * @param jsons  key传输的变量名    value为已经转好的json
	 * @param charset charset 编码格式 如UTF-8
	 * @return
	 */
	private static String toJson(HttpServletResponse response, Map<String , String> map ,String charset){
		//HttpServletResponse response =  getResponse();
		if(null == map || map.size()<1){
			return "";
		}
		StringBuilder text =new StringBuilder();
		Set<Map.Entry<String, String>> set = map.entrySet();
		text.append("{");
		for (Iterator<Map.Entry<String, String>> it = set.iterator(); it.hasNext();) {
	          Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
	          text.append("\"").append(entry.getKey()).append("\":");
	          text.append(entry.getValue());
	          text.append(",");
	    }
		PrintWriter out=null;
		try {
			response.setContentType("text/html;charset="+charset);
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0L);
			response.setCharacterEncoding(charset); 
			out =  response.getWriter();
			out.write(text.substring(0, text.length()-1)+"}");
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage(), e);
		}
		//response.getWriter().write(text);
		return null;
	}
	
	private static String toJson(HttpServletResponse response, Map<String , String> map){
	   return toJson(response,map,"UTF-8");
	}
	
	public static String toJson(Map<String,String> map){
		return toJson(ServletActionContext.getResponse(),map,"UTF-8");
	}
	
	
	private static String textString(HttpServletResponse response, String html ,String charset){
		//HttpServletResponse response =  getResponse(); 
		PrintWriter out=null;
		try {
			response.setContentType("text/html;charset="+charset);
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0L);
			response.setCharacterEncoding(charset); 
			out =  response.getWriter();
			out.write(html);
			out.flush();
			out.close();
		} catch (IOException e) { 
			log.error(e.getMessage(), e);
		} 
		return null;
	}
	
	
	public static String textString(HttpServletResponse response,String html){
		return textString(response,html,"UTF-8");
	}
	
	public static String textString(String html){
		return textString(ServletActionContext.getResponse(),html,"UTF-8");
	}
	
	public static String textString(String html,String charset){
		return textString(ServletActionContext.getResponse(),html,charset);
	}
	
	/**
	 * 特有的格式
	 * "{\"code\":\"0\",\"msg\":\"短信发送失败\"}"
	 * @param code
	 * @param msg
	 * @return
	 */
	
	public static String commonJson(Integer code,String msg){
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"code\":\"").append(code.toString()).append("\",\"msg\":\"").append(msg).append("\"}");
		return textString(ServletActionContext.getResponse(),buffer.toString(),"UTF-8");
	}
	
	/**
	 * 特有的格式
	 * "{\"code\":\"1\",\"msg\":\"短信已发送\",\"data\":{....}}"
	 * @param code
	 * @param msg
	 * @param data
	 * @return
	 */
	public static String commonJson(Integer code,String msg,String data){
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"code\":\"").append(code.toString()).append("\",\"msg\":\"").append(msg).append("\"");
		if(null!=data){
			//data是一个完整的JSON格式数据
			buffer.append(",\"data\":").append(data);
		}
		buffer.append("}");
		return textString(ServletActionContext.getResponse(),buffer.toString(),"UTF-8");
	}
	
	/**
	 * 特有的格式
	 * "{\"code\":\"1\",\"msg\":\"短信已发送\",\"data\":{....}}"
	 * @param code
	 * @param msg
	 * @param data
	 * @return
	 */
	public static String commonJson(Integer code,String msg,Object data){
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"code\":\"").append(code.toString()).append("\",\"msg\":\"").append(msg).append("\"");
		if(null!=data){
			//data是一个完整的JSON格式数据
			buffer.append(",\"data\":").append(JSON.toJSONString(data));
		}
		buffer.append("}");
		return textString(ServletActionContext.getResponse(),buffer.toString(),"UTF-8");
	} 
	
	
	/**
	 * easyUI格式数据
	 * "{\"rows\":{....}}"
	 * @param data
	 * @return
	 */
	public static String easyUIJson(Object data){ 
		return textString(JSON.toJSONString(data));
	}
	
}
